//
//  Post.swift
//  jmwebseoApp1803
//
//  Created by YQ05 on 2018/3/31.
//  Copyright © 2018年 YQ05. All rights reserved.
//

import Foundation
//import ObjectMapper
import Moya


struct PostIndexResponse: Codable {
    var status = ""
    var count : Int?
    var posts : [Posts]!
}

struct SubmitResponse: Codable {
    var status = ""
}

struct Comment: Codable {
    var name = ""
    var content = ""
    
}

struct Posts: Codable {
    var id = 0
    var title = ""
    
    var content = ""
    var url = ""
    var comment_count = 1
    var comments: [Comment]!
}

//
//struct PostIndexResponse: Mappable{
//    var status : String!
//    var count : Int!
//    var posts : [Posts]!
//
//
//    init?(map: Map) {
//
//    }
//    mutating func mapping(map: Map) {
//        status <- map["status"]
//        count <- map["count"]
//        posts <- map["posts"]
//    }
//}
//
//
//
//struct SubmitResponse: Mappable{
//    var status : String!
//
//
//
//    init?(map: Map) {
//
//    }
//    mutating func mapping(map: Map) {
//        status <- map["status"]
//
//    }
//}
//
//struct Comment: Mappable{
//    var name : String!
//    var content : String!
//
//    init?(map: Map) {
//
//    }
//
//    mutating func mapping(map: Map) {
//         content <- map["content"]
//         name <- map["name"]
//    }
//}
//
//
//
//
//struct Posts: Mappable{
//    var id: Int!
//    var status : String!
//    var count : Int!
//    var posts : String!
//    var title : String!
//
//    var content :String!
//    var url:String!
//    var comment_count : Int!
//    var comments :[Comment]!
//
////    var regprice : String!
////    var desc : String!
////    var author : [Any]?
////    var name : String!
//
//    init?(map: Map) {
//
//    }
//    mutating func mapping(map: Map) {
//        id <- map["id"]
//        status <- map["status"]
//        count <- map["count"]
//        posts <- map["posts"]
//        title <- map["title"]
//       comment_count <- map["comment_count"]
//
//       content <- map["content"]
//        url <- map["url"]
//
//        comments <- map["comments"]
//
////        desc <- map["desc"]
//     //  author <- map["author"]
//       //name = author![0] as! String
//    }
//}

extension Posts{
    
    //获取分类下文章列表
    static func request(id: Int , completion:@escaping ([Posts]?) -> Void){
        
        let provider = MoyaProvider<NetworkService>()
        
        provider.request(.showCateNewsList(id:id)) { result in
            switch result {
            case let .success(moyaResponse):
                
//                let json = try! moyaResponse.mapJSON() as! [String:Any]
//
//                if let jsonResponse = PostIndexResponse(JSON: json){
//                    print(jsonResponse.count,jsonResponse.status  + "。 posts网络返回结果")
//                    completion(jsonResponse.posts)
//                }
                
                do {
                    let decoder = JSONDecoder()
                    
                    let postIndexResponse = try decoder.decode(PostIndexResponse.self, from: moyaResponse.data)
                    //print("文章列表json:", postIndexResponse)
                    completion(postIndexResponse.posts)
                } catch  {
                    print("文章列表json解析错误：",error)
                }
            // do something with the response data or statusCode
            case  .failure:
                print("网络错误")
                completion(nil)
                
                // this means there was a network failure - either the request
                // wasn't sent (connectivity), or no response was received (server
                // timed out).  If the server responds with a 4xx or 5xx error, that
                // will be sent as a ".success"-ful response.
            }
        }
        
    }
    
    //发表文章的评论
    static func submitComment(postId: Int, name: String, email: String, content: String, completion:@escaping (Bool) -> Void){
        
        let provider = MoyaProvider<NetworkService>()
        
        provider.request(.submitComment(postId: postId, name: name, email: email, content: content)) { result in
            switch result {
            case let .success(moyaResponse):
//
//                let json = try! moyaResponse.mapJSON() as! [String:Any]
//
//
//                if let jsonResponse = SubmitResponse(JSON: json){
//                    if jsonResponse.status == "ok"{
//                        completion(true)
//                    }else{
//                        completion(false)
//                    }
//
//                }
                
                do {
                    let decoder = JSONDecoder()
                    
                    let submitResponse = try decoder.decode(SubmitResponse.self, from: moyaResponse.data)
                    print("提交评论json:", submitResponse)
                    
                    if submitResponse.status == "ok" {
                        completion(true)
                    } else {
                        completion(false)
                    }
                    
                    
                } catch  {
                    print("json解析错误",error)
                }
                
                
            // do something with the response data or statusCode
            case  .failure:
                print("网络错误")
                completion(false)
                
                
               
     
            }
        }
        
    }
}

//func getprocateList(id: Int){
//    let provider = MoyaProvider<NetworkService>()
//    provider.request(.showCateNewsList(id: id)) { (result) in
//        switch result {
//        case let .success(moyaResponse):
//            let json = try! moyaResponse.mapJSON() as! [String:Any]
//            if let jsonResponse = PostIndexResponse(JSON: json){
//                print(jsonResponse.posts)
//                print("结束啦")
//            }
//            
//            
//        case .failure:
//            print("wangluocuowu2")
//            
//        }
//    }
//}

